package com.example.yls.demoa;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by yls on 2004/1/1.
 */

public class StuAdapter extends BaseAdapter {

    private List<Student> studentList;
    private Context context;
    private IOnDataChangeListener onDataChangeListener;

    public StuAdapter(List<Student> studentList, Context context,IOnDataChangeListener onDataChangeListener) {
        this.studentList = studentList;
        this.context = context;
        this.onDataChangeListener = onDataChangeListener;
    }

    @Override
    public int getCount() {
        return studentList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        StuHolder holder;
        if(view==null){
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup, false);
            holder = new StuHolder();
            holder.stu_no = view.findViewById(R.id.stuno);
            holder.stu_name = view.findViewById(R.id.name);
            holder.stu_age = view.findViewById(R.id.age);
            holder.del = view.findViewById(R.id.del);

            view.setTag(holder);
        }

        holder = (StuHolder) view.getTag();
        Student student = studentList.get(i);
        holder.stu_no.setText(student.getStuno());
        holder.stu_name.setText(student.getName());
        holder.stu_age.setText(String.valueOf(student.getAge()));

        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onDataChangeListener.delStu(studentList.get(i));
            }
        });

        return view;
    }
}
